package br.ufc.util;

public class NDimensionalArray {
	private Object[] array; 
	private int[] dimensions; 
	private int[] multipliers; 

	public NDimensionalArray(int... dimensions) {
		int arraySize = 1;

		multipliers = new int[dimensions.length];
		for (int idx = dimensions.length - 1; idx >= 0; idx--) {
			multipliers[idx] = arraySize;
			arraySize *= dimensions[idx];
		}
		array = new Object[arraySize];
		this.dimensions = dimensions;
	}
	public Object get(int... indices) {
		assert indices.length == dimensions.length;
		int internalIndex = 0;

		for (int idx = 0; idx < indices.length; idx++) {
			internalIndex += indices[idx] * multipliers[idx];
		}
		return array[internalIndex];
	}
	public void set(int indices[], Object value) {
		assert indices.length == dimensions.length;
		int internalIndex = 0;

		for (int idx = 0; idx < indices.length; idx++) {
			internalIndex += indices[idx] * multipliers[idx];
		}
		array[internalIndex] = value;
	}
}